import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'package:http/retry.dart';

///
request1() async {
  var url = Uri.parse('https://jsonplaceholder.typicode.com/posts');
  var response = await http.get(url);
  if (response.statusCode == 200) {
    var jsonResponse = convert.jsonDecode(response.body) as List<dynamic>;
    print("the have ${jsonResponse.length} item in response");
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }
}

///
request2() async {
  var client = http.Client();
  try {
    var response = await client.get(
      Uri.parse('https://jsonplaceholder.typicode.com/posts'),
    );
    if (response.statusCode == 200) {
      var jsonResponse = convert.jsonDecode(response.body) as List<dynamic>;
      print("the have ${jsonResponse.length} item in response");
    } else {
      print('Request failed with status: ${response.statusCode}.');
    }
  } finally {
    client.close();
  }
}

///
class CustomClient extends http.BaseClient {
  final http.Client _inner;
  CustomClient(this._inner);

  @override
  Future<http.StreamedResponse> send(http.BaseRequest request) {
    request.headers['user-agent'] = 'CustomClient/1.0';
    request.headers['accept'] = 'application/json';
    request.headers['content-type'] = 'application/json';
    return _inner.send(request);
  }
}

///
request3() async {
  final client = RetryClient(http.Client());
  try {
    print(await client.read(Uri.http('example.org', '')));
  } finally {
    client.close();
  }
}

///
